<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <!-- 引入jquery CDN -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="script.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        a {
            text-decoration: none;
        }

        html {
            height: 100%;
        }

        body {
            height: 100%;
            background: #fff url(images/page.png) 50% 50% no-repeat;
            background-size: cover;
            background-color: #F7F8F9;
            overflow: hidden;
        }

        .dowebok {
            width: 400px;
            height: 400px;
            margin: 200px auto 0;
            background-color: #fff;
            border-radius: 10px;
        }

        .register {
            text-align: center;
            margin: 30px auto 20px;
            font: normal 700 25px/75px Microsoft YaHei;
        }

        input {
            display: block;
            border-radius: 5px;
            width: 330px;
            height: 50px;
            margin: 20px auto;
            border: 2px solid #F0F0F0;
            text-indent: 2em;
            font: normal 400 15px/50px Microsoft YaHei;
            color: #8F9DBE;
            background-color: transparent;
            outline: none;
        }

        .dowebok .line {
            width: 334px;
            height: 30px;
            margin: 20px auto;
        }

        .dowebok .line .reg {
            float: left;
            font-size: 15px;
            line-height: 30px;
            color: #7098FE;
        }

        .line a:hover {
            color: #3b78dd;
        }

        button {
            display: block;
            width: 334px;
            height: 50px;
            margin: 20px auto;
            border: 0;
            background-color: #1760D7;
            font: normal 700 23px/50px Microsoft YaHei;
            color: #fff;
        }
    </style>
</head>
<body>
    <div class="dowebok">
        <div class="register">
            账号注册
        </div>
        <input id="user" type="text" autocomplete="off" placeholder="账号">
        <input id="passwd" type="password" autocomplete="off" placeholder="密码">
        <div class="line">
            <a href="sign in.html" class="reg">立即登录</a>
        </div>
        <button onclick="submit()">注册</button>
    </div>
    <script>
        function submit() {
            // 获取信息
            let username = $(".dowebok #user").val();
            if (username == '') {
                alert('账号不能为空');
                return;
            }
            let password = $(".dowebok #passwd").val();
            if (password == '') {
                alert('密码不能为空');
                return;
            }
            // console.log(username);
            // console.log(password);
            // 发起ajax请求
            $.ajax({
                method: 'Post',
                url: '/register',
                dataType: 'json',
                contentType: 'application/json;charaset=utf-8',

                data: JSON.stringify({
                    'user': username,
                    'password': password
                }),

                success: function (data) {
                    if (data.result == 'success') {
                        alert('账号注册成功');
                        location.href = "/sign in.html";
                        window.event.returnValue = false;
                    }
                    else {
                        alert('注册失败，用户已存在');
                    }
                }
            });
        }
    </script>
</body>
</html>